package com.zhc.dejingapi.config;

import com.bstek.ureport.definition.datasource.BuildinDatasource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * https://segmentfault.com/a/1190000024556840?sort=votes
 * Ureport 数据源
 *
 * 学习教程 https://blog.csdn.net/lige12/article/details/131829472
 * @author zhouzhaodong
 */

@Component
@Slf4j
public class UreportDataSource implements BuildinDatasource {
    private static final String NAME = "MyDataSource";
    //private final Logger log = LoggerFactory.getLogger(UreportDataSource.class);

    //@Resource
    @Qualifier("mySqlDataSource")
    private  DataSource dataSource;

    public UreportDataSource(@Qualifier("mySqlDataSource")DataSource dataSource) {
        this.dataSource = dataSource;
    }

    /**
     * 数据源名称
     **/
    @Override
    public String name() {
        return NAME;
    }

    /**
     * 获取连接
     **/
    @Override
    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            log.error("Ureport 数据源 获取连接失败！");
            e.printStackTrace();
        }
        return null;
    }

}